﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace Boggle
{
    /// <summary>
    /// Represents a word in a game of Boggle.
    /// </summary>
    public partial class Word : INotifyPropertyChanged
    {
        /// <summary>
        /// The text of the word.
        /// </summary>
        private readonly String wordText;

        /// <summary>
        /// The dice used to make up the word.
        /// </summary>
        private readonly IList<Die> dice;

        /// <summary>
        /// A definition of the word.
        /// </summary>
        private String definition;

        /// <summary>
        /// The status of the word.
        /// </summary>
        private Status status;

        /// <summary>
        /// The score awarded to the word based on its length and status.
        /// </summary>
        private Int32 score;

        /// <summary>
        /// Gets the text used to make up this word.
        /// </summary>
        public String WordText { get { return wordText; } }

        /// <summary>
        /// Gets the dice used to make up this word.
        /// </summary>
        public IList<Die> Dice { get { return dice; } }

        /// <summary>
        /// Gets or sets the definition of this word.
        /// </summary>
        public String Definition
        { 
            get { return definition; }
            set
            {
                if (definition != value)
                {
                    definition = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("Definition"));
                }
            }
        }

        /// <summary>
        /// Gets or sets the status of this word.
        /// </summary>
        public Status CurrentStatus
        {
            get { return status; }
            set
            {
                if (status != value)
                {
                    status = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("CurrentStatus"));
                    Score = ScoreWord();
                }
            }
        }

        /// <summary>
        /// Gets the score awarded to this word.
        /// </summary>
        public Int32 Score
        {
            get { return score; }
            private set
            {
                if (score != value)
                {
                    score = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("Score"));
                }
            }
        }

        /// <summary>
        /// Creates a new word.
        /// </summary>
        /// <param name="wordText">The text making up the word.</param>
        /// <param name="dice">The dice used to make up the word.</param>
        public Word(String wordText, IList<Die> dice)
        {
            if (wordText == null)
            {
                throw new ArgumentNullException("wordText");
            }
            if (dice != null && dice.Aggregate("", (acc, d) => acc + d.CurrentValue) != wordText)
            {
                throw new ArgumentException("dice must represent the letters in the word");
            }

            this.wordText = wordText;
            this.dice = dice == null ? null : new ReadOnlyCollection<Die>(dice);
        }

        /// <summary>
        /// Scores the word based upon its current status and length.
        /// </summary>
        /// <returns>The score this word should receive.</returns>
        private int ScoreWord()
        {
            if (CurrentStatus != Status.Validated)
            {
                return 0;
            }
            if (WordText.Length <= 2)
            {
                return 0;
            }
            if (WordText.Length <= 4)
            {
                return 1;
            }
            if (WordText.Length == 5)
            {
                return 2;
            }
            if (WordText.Length == 6)
            {
                return 3;
            }
            if (WordText.Length == 7)
            {
                return 5;
            }

            return 11;
        }

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Raised when a public property changes value.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Raises the PropertyChanged event.
        /// </summary>
        /// <param name="e">The arguments for the event.</param>
        private void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        #endregion
    }
}
